package com.gm.config;

import com.gm.filter.MyFilter;
import com.gm.listener.MyListener;
import com.gm.servlet.MyServlet;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Servlet;
import javax.servlet.ServletContextListener;
import java.util.Arrays;
import java.util.EventListener;

/**
 * @Author Administrator
 * @Date 2020/6/12 14:46
 **/
@Configuration
public class MyServletConfig {
    //serverProperties 在底层最终会实现WebServerFactoryCustomizer 变得更好风景好就好看了，
    @Bean
    public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> myServer(){
        return new WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>(){
            @Override
            public void customize(ConfigurableServletWebServerFactory factory){
                factory.setPort(8083);
            }
        };
    }
    //servlet
    @Bean
    public ServletRegistrationBean<MyServlet> myServlet(){
        ServletRegistrationBean<MyServlet> myServlet = new ServletRegistrationBean<>(new MyServlet(),"/MyServlet");
        return myServlet;
    }
    //Filter
    @Bean
    public FilterRegistrationBean<MyFilter> myFilter(){
        FilterRegistrationBean<MyFilter> myFilter = new FilterRegistrationBean<>();
        myFilter.setFilter(new MyFilter());
        myFilter.setUrlPatterns(Arrays.asList("/hello","/MyFilter"));
        return myFilter;
    }
    //Listener
    @Bean
    public ServletListenerRegistrationBean<MyListener> myListener(){
        ServletListenerRegistrationBean<MyListener> myListener = new ServletListenerRegistrationBean<>();
        myListener.setListener(new MyListener());
        return myListener;
    }
}
